package com.example.yunxinagyue.info_user

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import com.example.yunxinagyue.Logger
import com.example.yunxinagyue.R

open class MainActivity2 : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        setActionBarStyle()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.user_msg,menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.info -> {
                val intent= Intent(this,UserInfo::class.java)
                startActivityForResult(intent,1)
            }
            R.id.QR_code -> {
                val intent= Intent(this,UserQrCode::class.java)
                startActivity(intent)
            }
            R.id.background -> {
                val intent= Intent(this,Cover::class.java)
                startActivityForResult(intent,1)
            }
            android.R.id.home -> finish()

        }
        return super.onOptionsItemSelected(item)
    }


    open fun setActionBarStyle(){
        val actionBar=supportActionBar
        if(actionBar!=null){
//            设置ActionBar是否显示返回键
            actionBar.setDisplayHomeAsUpEnabled(true)
//            导入返回键图片
            actionBar.setHomeAsUpIndicator(R.drawable.ic_return)
//            设置是否显示标题
//            actionBar.setDisplayShowTitleEnabled(false)
        }
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.name -> {
                val intent=Intent(this,RectifyName::class.java)
                registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
                    if (result.resultCode != Activity.RESULT_OK) {
                        Logger.error(TAG, "error when launch activity: $result")
                    }
                }.launch(intent)
            }
        }
    }

    companion object {
        private const val TAG = "MainActivity2"
    }

}